package com.example.rabbitmqdemo.details;

import com.example.rabbitmqdemo.util.ConnectionUtils;
import com.rabbitmq.client.*;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.concurrent.TimeoutException;

public class MyReceiver {

    private static final String QUEUE_NAME = "my_queue";
    private static final String ALTERNATE_QUEUE_NAME = "my_alternate_queue";
    private static final String EXCHANGE_NAME = "my_exchange";
    private static final String ALTERNATE_EXCHANGE_NAME = "my_alternate_exchange";

    public static void main(String[] args) throws IOException, TimeoutException {

        Connection connection = ConnectionUtils.getConnection();
        Channel channel = connection.createChannel();

        channel.queueDeclare(QUEUE_NAME, true, false, false, null);
        channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "r");

        channel.queueDelete(ALTERNATE_EXCHANGE_NAME);
        HashMap<String, Object> map = new HashMap<>();
        map.put("x-message-ttl", 10000);
        channel.queueDeclare(ALTERNATE_QUEUE_NAME, true, false, false, map);
        channel.queueBind(ALTERNATE_QUEUE_NAME, ALTERNATE_EXCHANGE_NAME, "r1");

        channel.basicConsume(QUEUE_NAME, false, new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope,
                                       AMQP.BasicProperties properties, byte[] body) throws IOException {
                String routingKey = envelope.getRoutingKey();
                long deliveryTag = envelope.getDeliveryTag();
                String exchange = envelope.getExchange();
                String contentType = properties.getContentType();
                Integer deliveryMode = properties.getDeliveryMode();
                Integer priority = properties.getPriority();
                String receivedMsg = new String(body, StandardCharsets.UTF_8);
                System.out.println("exchange: " + exchange);
                System.out.println("received: " + receivedMsg);
                System.out.println("routingKey: " + routingKey);
                System.out.println("contentType: " + contentType);
                System.out.println("deliveryMode: " + deliveryMode);
                System.out.println("priority: " + priority);
                System.out.println();
                channel.basicAck(deliveryTag, false);
            }
        });
        channel.basicConsume(ALTERNATE_QUEUE_NAME, false, new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope,
                                       AMQP.BasicProperties properties, byte[] body) throws IOException {
                String routingKey = envelope.getRoutingKey();
                long deliveryTag = envelope.getDeliveryTag();
                String exchange = envelope.getExchange();
                String contentType = properties.getContentType();
                Integer deliveryMode = properties.getDeliveryMode();
                Integer priority = properties.getPriority();
                String receivedMsg = new String(body, StandardCharsets.UTF_8);
                System.out.println("exchange: " + exchange);
                System.out.println("received: " + receivedMsg);
                System.out.println("routingKey: " + routingKey);
                System.out.println("contentType: " + contentType);
                System.out.println("deliveryMode: " + deliveryMode);
                System.out.println("priority: " + priority);
                System.out.println();
                channel.basicAck(deliveryTag, false);
            }
        });

//        channel.close();
//        connection.close();
    }
}
